<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\wxapp\controller;

use cmf\controller\RestBaseController;
use think\Db;
use wxapp\aes\WXBizDataCrypt;

class UserController extends RestBaseController{


    // 获取用户信息
    public function getUserInfo() {

        $uid=$this->getUserId();

        $user=Db::name('user')->where(['id'=>$uid])->find();

        if  ($user['agentid']){

            $puser=Db::name('user')->where(['id'=>$user['agentid']])->find();
            $user['tjr']=$puser['name'];
        }


        $this->success('获取用户信息',$user);
    }

    /**
     * 认证
     */
    public function renzhen(){
        $uid=$this->getUserId();

        $data=$this->request->param();


        $mobile=$data['pmobile'];

        $puse=Db::name('user')->where(['mobile'=>$mobile])->where('level','gt',0)->find();

        if (!$puse){
            $this->error('没有相应团长信息');
        }

        $data['agentid']=$puse['id'];


        Db::name('user')->where(['id'=>$uid])->update($data);

        $this->success('认证成功',$data);

    }

    public function getorderbyid(){

        $id=$this->request->param('id');

        $goods=Db::name('aa_goods')->where(['id'=>$id])->find();

        $goods['list']=explode(',',$goods['desc']);

        $this->success('商品详情',$goods);

    }

    /**
        用户报单
     */

    public function addorder(){

        $uid=$this->getUserId();
        $data=$this->request->param();
        $data['uid']=$uid;
        $data['times']=time();

            unset($data['qy']);

        $user=Db::name('user')->where(['id'=>$uid])->find();

        if (!$data['name']||!$data['goods']||!$data['address']||!$data['money']||!$data['zfb']){
            $this->error('请填写完整信息');
        }





        $goods=Db::name('aa_goods')->where(['id'=>$data['gid']])->find();

        if  ($goods['status']>2){
                      $this->error('商品已经取完,无法修改');
        }


        $data['types']=$goods['types'];

        $data['pid']=$user['agentid'];
        $data['team_id']=$user['team_id'];
        Db::name('aa_order')->data($data)->insert();

        $this->success('报单成功');
    }


    /**
     * 我的订单
     */
    public function getmyorder(){

        $uid=$this->getUserId();
        $page=$this->request->param('page');

        $map['uid']=['eq',$uid];



        $list=Db::name('aa_order')
            ->where($map)
            ->order('id desc')
            ->select()
            ->toArray();

        foreach ($list as &$key){

            $key['times']=date('Y-m-d H:i',$key['times']);

        }

        $this->success('我的订单',$list);
    }


    /**
     * 我的订单
     */
    public function getordrbyid(){

        $id=$this->request->param('id');

        $list=Db::name('aa_order')
            ->where(['id'=>$id])
            ->find();


        $goods=Db::name('aa_goods')->where(['id'=>$list['gid']])->find();

        $good=explode(',',$goods['desc']);

        $list['good']=$good;

        $this->success('订单详情',$list);
    }

    /**
        修改订单
     */
    public function saveorder(){



        $uid=$this->getUserId();
        $data=$this->request->param();

        if (!$data['name']||!$data['goods']||!$data['address']||!$data['money']||!$data['zfb']){
            $this->error('请填写完整信息');
        }

        $id=intval($data['id']);
        unset($data['id']);

         $info=Db::name('aa_order')->where(['id'=>$id])->find();


        if (!$info||$info['uid']!=$uid){

            $this->error('无权限');
        }

         $goods=Db::name('aa_goods')->where(['id'=>$info['gid']])->find();

         if  ($goods['status']>2){
                 $this->error('商品已经取完,无法修改');
         }

         if  ($info['status']>0){
                 $this->error('订单状态也无法修改!');
         }




        Db::name('aa_order')->where(['id'=>$info['id']])->update($data);

        $this->success('修改订单');

    }

    public function getmydown(){

        $id=$this->getUserId();

        $list=Db::name('user')->where(['agentid'=>$id])->select()->toArray();

        foreach ($list as &$key){

            $key['create_time']=date('Y-m-d H:i',$key['create_time']);
        }

        $this->success('我的下级',$list);
    }


    /**
     * 入库
     */

    public function ruku(){

        $id=$this->getUserId();
        $user=Db::name('user')->where(['id'=>$id])->find();

        $logs=Db::name('aa_order')
            ->where('team_id','eq',$user['team_id'])
           ->find();

        if(!$logs){
            $this->error('无权限！');
        }

        $code=$this->request->param('code');
        
         $log=Db::name('aa_order')
             ->where('team_id','eq',$user['team_id'])
             ->where(['kdnum'=>$code])
             ->whereOr(['kdnumb'=>$code])->find();

         if($log&&$log['status']==1){
                      $this->error('已入库');
         }

        $ret=Db::name('aa_order')->where(['kdnum'=>$code])->whereOr(['kdnumb'=>$code])
            ->update(['status'=>1,'rutime'=>date('y-m-d H：i').'扫码入库','rutime2'=>time()]);

        if (!$ret){



            $this->error('操作失败');
        }else{

            $this->success($log['goods'].'操作成功');
        }

    }


    public function signature(){

        $id=$this->getUserId();

        $value=$this->request->param('value');


        $ret=Db::name('user')->where(['id'=>$id])->update(['signature'=>$value]);

    }



    /**
    查询相商品关订单
     */
    public function getorderbygid(){

        $gid=$this->request->param('id');

        $uid=$this->getUserId();

        $list=Db::name('aa_order')->where(['gid'=>$gid])
            ->where(['pid'=>$uid])
        ->select()->toArray();

        foreach ($list as &$key){

            $user=Db::name('user')->where(['id'=>$key['uid']])->find();

            $key['username']=$user['name'];
            $key['usermobile']=$user['mobile'];
            $key['avatar']=$user['avatar'];
            $key['times']=date('Y-m-d H:i',$key['times']);
        }

        $this->success('订单信息',$list);

    }


   //  删除  123222
    public function deletebyid(){
        
        $id=$this->request->param('id');
        
        $info=Db::name('aa_order')->where(['id'=>$id])->find();

        $goods=Db::name('aa_goods')->where(['id'=>$info['gid']])->find();

        if  ($goods['status']>1){
                $this->error('商品已经取完,无法修改');
        }

        if  ($info['status']>0){
                $this->error('商品已经取完,无法修改');
        }


        Db::name('aa_order')->where(['id'=>$id])->delete();
        
        

        $this->success('订单信息');
    }

    //团队订单

    public function addtameorder(){

        $uid=$this->getUserId();
        $data=$this->request->param();
        $data['uid']=$uid;

        if (empty($data['name'])||empty($data['mobile'])||empty($data['number'])||empty($data['goodname'])){
            $this->error('请输入完整信息1');
        }


        if ($data['backtype']==2&&empty($data['kdorder'])){
            $this->error('请输入完整信息2');
        }

        if ($data['backtype']!=2&&empty($data['dates'])){
            $this->error('请输入完整信息3');
        }


        $data['timea']=time();
        Db::name('aa_tame_order')->data($data)->insert();

        $this->success('提交成功');
    }

    /**
        我的团队订单
     */

    public function getmytameorder(){
        $uid=$this->getUserId();

        $listd=Db::name('aa_tame_order')->where(['uid'=>$uid])
            ->select()->toArray();

        foreach ($listd as &$ke){
            $ke['timea']=date('Y-m-d',$ke['timea']);

        }


        $this->success('提交成功',$listd);
    }


    /**
     * 删除信息
     */
    public function deletetameorder(){
        $uid=$this->getUserId();
        $id=$this->request->param('id');

        Db::name('aa_tame_order')->where(['uid'=>$uid])
            ->where('id','eq',$id)
            ->where('status','eq',0)
            ->delete();


        $this->success('提交成功');

    }

    //用户绑定团队

    public function bandteam(){

        $uid=$this->getUserId();
        $id=$this->request->param('id');

       $us= Db::name('user')->where(['id'=>$uid])
            ->find();

       if ($us['team_id']){
           $this->error('重复提交');
       }

        Db::name('user')->where(['id'=>$uid])
            ->update(['team_id'=>$id]);


        $this->success('提交成功');


    }



    /**
    商品信息 团队用
     */
    public function indexlistbypage(){

        $page=$this->request->param('page');
        $limits=($page-1)*10 . ',15';

        $id=$this->getUserId();

        $user=Db::name('user')->where(['id'=>$id])->find();

        $list=Db::name('aa_goods')
            ->where('id','gt',0)
            ->where('team_id','eq',$user['team_id'])
            ->limit($limits)
            ->order('id desc')
            ->select()->toArray();

        foreach ($list as &$key){
            $key['times']=date('Y-m-d H:i',$key['times']);
        }

        $this->success('首页信息',$list);

    }



}
